﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System.Net;

namespace Armon.Lib.Web.Filters
{
    public class HttpResponseFilter : IActionFilter
    {

        public void OnActionExecuting(ActionExecutingContext context) { }

        public void OnActionExecuted(ActionExecutedContext context)
        {
            if (context.Result is not ObjectResult)
            {
                return;
            }

            var result = new
            {
                Success = true,
                Result = (context.Result as ObjectResult)?.Value
            };
            context.Result = new ObjectResult(result)
            {
                StatusCode = (int)HttpStatusCode.OK,
            };
        }
    }
}
